import com.sun.media.jfxmediaimpl.HostUtils;

import java.sql.SQLOutput;

import static java.lang.Thread.sleep;

class mythread extends Thread {
	@Override
	public void run() {
		while(true) {
			System.out.println("hello thread");
			try {
				sleep(1000);
			} catch (InterruptedException e) {
				throw new RuntimeException(e);
			}
		}
	}
}
class MyRunnable implements Runnable {
	@Override
	public void run() {
		while(true) {
			System.out.println("hello thread");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				throw new RuntimeException(e);
			}
		}
 	}
}
public class Demo1 {
	private static int  cnt=100;
	public static void main(String[] args) throws InterruptedException {
		//实现Runnable的匿名内部类的创建线程。
		Thread t=new Thread(new Runnable() {
			@Override
			public void run() {
				while(true) {
					System.out.println("11111111");
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						throw new RuntimeException(e);
					}
				}
			}
		});
		t.start();
		while(true) {
			System.out.println("222222222222222222222");
			Thread.sleep(1000);
		}

	}
	public static void main5(String[] args) {
		Thread t=new Thread(()->{
				System.out.println("线程开始");
				System.out.println("hello thread");
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					throw new RuntimeException(e);
				}
				System.out.println("线程结束");
		},"这是新线程");
		System.out.println(t.isAlive());
		t.start();
		System.out.println(t.isAlive());
		try{
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			throw new RuntimeException(e);
		}
		System.out.println(t.isAlive());
	}
	public static void main4(String[] args) {
		Thread t=new Thread(()->{
			while(true) {
				System.out.println("hello thread");
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					throw new RuntimeException(e);
				}
			}
		},"这是新线程");
		//将它设置为后台进程，此时main这个前台线程会在一瞬间执行完毕，后台线程来不及执行就结束了
		t.setDaemon(true);
		t.start();
	}
	public static void main3(String[] args) throws InterruptedException {
		//这是在main线程里面又创建一个线程
		//创建的线程的第三种方式
//		Thread t=new Thread() {
//			@Override
//			public void run(){
//				while(true) {
//					System.out.println("hello thread");
//					try {
//						Thread.sleep(1000);
//					} catch (InterruptedException e) {
//						throw new RuntimeException(e);
//					}
//				}
//			}
//		};
		//lambda的表达式运用
		//这种方式相当于new Runnable，Thread实现了这个接口
		//创建线程第四种方式
		Thread t=new Thread(()-> {
			while(true) {
				System.out.println("hello thread");
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					throw new RuntimeException(e);
				}
			}
		});
		t.start();
		while(true) {
			System.out.println("hello main");
			Thread.sleep(1000);
		}

	}
	//创建线程第二种方式
	public static void main2(String[] args) throws InterruptedException {
		MyRunnable myRunnable=new MyRunnable();
		Thread t=new Thread(myRunnable);
		t.start();
		while(true) {
			System.out.println("hello main");
			Thread.sleep(1000);
		}
	}
	//创建线程第一种方式
	public static void main1(String[] args) throws InterruptedException {
		Thread t=new mythread();
		t.start();
		while(true) {
			System.out.println("hello main");
			//sleep(1000);
			Thread.sleep(1000);
			//以上两种方式都可以
		}
	}
}
